# -*- coding: utf-8 -*-
import scrapy
import re
import json
from ..items import JDItem

class JdbraSpider(scrapy.Spider):
    name = 'Jdbra'
    #allowed_domains = ['www.jd.com']
    start_urls = ['http://www.jd.com/']
    url = 'https://search.jd.com/Search?keyword=%E8%83%B8%E7%BD%A9&enc=utf-8&wq=%E8%83%B8%E' \
          '7%BD%A9&pvid=4df1cb9df979419aaf4fd9e7b61d4496'

    def start_requests(self):
        yield scrapy.Request(url=self.url,callback=self.parse_product)

    def parse_product(self,response):
        productIDs = response.css('.gl-item::attr(data-sku)').extract()
        productIDs = list(set(productIDs))
        for productID in productIDs:
            url_comment = 'https://sclub.jd.com/comment/productPageComments.action?' \
                          'callback=fetchJSON_comment98vv803&productId='+ productID +\
                          '&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1'
        yield scrapy.Request(url=url_comment,callback=self.parse_comment_page)

    def parse_comment_page(self,response):
        productID = re.search(r'"productId":(\d+),',response.text).group(1)
        maxPage = int(re.search(r'"maxPage":(\d+),',response.text).group(1))
        for pn in range(maxPage):
            url_comment = 'https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv803&productId='\
                          +productID + '&score=0&sortType=5&page='+str(pn)+'&pageSize=10&isShadowSku=0&rid=0&fold=1'
            yield scrapy.Request(url=url_comment,callback=self.parse_comment)

    def parse_comment(self,response):
        html = response.text
        html = html.replace('fetchJSON_comment98vv803(','')
        html = html.replace(');','')
        comment = json.loads(html)
        results = comment['comments']
        for i in range(len(results)):
            item = JDItem()
            item['content'] =results[i]['content']
            item['creationTime']=results[i]['creationTime']
            item['id'] = results[i]['id']
            item['productColor'] = results[i]['productColor']
            item['productSize'] = results[i]['productSize']
            item['score'] = results[i]['score']
            item['userClientShow'] = results[i]['userClientShow']
            item['userLevelName'] = results[i]['userLevelName']
            yield item

    def parse(self, response):
        pass
